package com.proton.carepatchtemp.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.activity.PrfReprotActivity;
import com.proton.carepatchtemp.activity.ProfileEditActivity;
import com.proton.carepatchtemp.adapterholder.ProfileHolder;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.RealData;
import com.proton.carepatchtemp.database.TestManager;
import com.proton.carepatchtemp.database.table.Profile;
import com.proton.carepatchtemp.utils.net.PrfCenter;
import com.proton.carepatchtemp.utils.ui.ToastUtils;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import cn.pedant.SweetAlert.Type;

/**
 * Created by MoonlightSW on 2016/11/24.
 */

public class ProfileAdapter extends RecyclerView.Adapter<ProfileHolder> {

    public List<Profile> mProfiles;
    private Context mContext;
    private LayoutInflater mInflater;

    public ProfileAdapter(Context context, List<Profile> profiles) {
        this.mContext = context;
        this.mProfiles = profiles;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public ProfileHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_profile, parent, false);
        return new ProfileHolder(view);
    }

    @Override
    public void onBindViewHolder(ProfileHolder holder, int position) {
        holder.mSdvProfileAvatar.setImageURI(mProfiles.get(position).getAvatar());
        holder.mTvProfileName.setText(mProfiles.get(position).getRealname());
        if (!TextUtils.isEmpty(mProfiles.get(position).getBirthday())) {
            holder.mTvAge.setText(mProfiles.get(position).getAge() + mContext.getString(R.string.sui_2));
        }

        if (1 == mProfiles.get(position).getGender()) {
            holder.mIvProfileSex.setImageResource(R.mipmap.profile_boy);
        } else {
            holder.mIvProfileSex.setImageResource(R.mipmap.profile_girl);
        }

        holder.mTvEditProfile.setOnClickListener(view -> {
            Intent goEdit = new Intent(mContext, ProfileEditActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(Extras.PRF_EDIT, mProfiles.get(position));
            goEdit.putExtras(bundle);
            mContext.startActivity(goEdit);
        });

        holder.mTvProfileReport.setOnClickListener(view -> {
            Intent goReport = new Intent(mContext, PrfReprotActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString(Extras.PROFILE, String.valueOf(mProfiles.get(position).getId()));
            goReport.putExtras(bundle);
            mContext.startActivity(goReport);

        });

        holder.mTvDeleteProfile.setOnClickListener(view -> {

            // reuse previous dialog instance, keep widget user state, reset them if you need
// or you can new a SweetAlertDialog to show
/* sDialog.dismiss();
 new SweetAlertDialog(SampleActivity.this, SweetAlertDialog.ERROR_TYPE)
         .setTitleText("Cancelled!")
         .setContentText("Your imaginary file is safe :)")
         .setConfirmText("OK")
         .show();*/
            new SweetAlertDialog(mContext, Type.WARNING_TYPE)
                    .setTitleText(mContext.getString(R.string.string_confirm_delete))
                    .setContentText(mContext.getString(R.string.string_delete_profile_msg))
                    .setCancelText(mContext.getString(R.string.string_cancel))
                    .setConfirmText(mContext.getString(R.string.string_confirm))
                    .showCancelButton(true)
                    .setCancelClickListener(Dialog::dismiss)
                    .setConfirmClickListener(sDialog -> {

                        for (RealData rti : TestManager.container.values()
                                ) {
                            if (rti.getProfile().getId().longValue() == mProfiles.get(position).getId().longValue()) {
                                ToastUtils.w(R.string.string_is_measureing_can_not_delete);
                                return;
                            }
                        }
                        PrfCenter.delete(String.valueOf(mProfiles.get(position).getId()));
                        mProfiles.remove(position);
                        notifyDataSetChanged();
                        sDialog.setTitleText(mContext.getString(R.string.string_deleted))
                                .setContentText(mContext.getString(R.string.string_select_profile_delete_success))
                                .setConfirmText(mContext.getString(R.string.string_confirm))
                                .showCancelButton(false)
                                .setCancelClickListener(null)
                                .setConfirmClickListener(null)
                                .changeAlertType(Type.SUCCESS_TYPE);
                    })
                    .show();
        });
    }

    @Override
    public int getItemCount() {
        return mProfiles == null ? 0 : mProfiles.size();
    }
}
